package org.yi.fc.entity;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;

public class FSysUser extends Model<FSysUser>{
	
	private static final long serialVersionUID = -4368208647272740379L;
	
	public static final FSysUser dao = new FSysUser();
	
	/**
	 * 根据邮箱和密码获取用户
	 * @param username
	 * @param pw
	 * @return
	 */
	public List<FSysUser> get(String username, String pw){
		//TODO 空校验或者SQL注入校验
		String sql = "select * from f_sys_user where username = ? and passwd = ?";
		List<FSysUser> list = dao.find(sql, username, pw);
		return list;
	}
	
	/**
	 * 根据邮箱和密码获取用户
	 * @param username
	 * @param pw
	 * @return
	 */
	public FSysUser uniqueResult(String username, String pw){
		//TODO 空校验或者SQL注入校验
		String sql = "select * from f_sys_user where username = ? and passwd = ?";
		List<FSysUser> list = dao.find(sql, username, pw);
		//list永远不为null， 所以无需判断
		if(list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	/**
	 * 判断用户是否存在， 存在返回true， 否则返回false
	 * @param username
	 * @return
	 */
	public boolean userExist(String username) {
		String sql = "select count(*) from f_sys_user where username = ?";
		return Db.queryLong(sql, username) > 0;
	}
	
	/**
	 * 根据用户名获取用户
	 * @param username
	 * @return
	 */
	public FSysUser getUserByName(String username){
		String sql = "select * from f_sys_user where username = ?";
		return findFirst(sql,username);
	}

}
